﻿using Wpg_VoteSys.Models;
using Wpg_VoteSys.Models.Database;
using Wpg_VoteSys.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Wpg_VoteSys.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly Wpg_VoteSysContext _db;
        private readonly IJWTService _jwtService;

        public UserController(Wpg_VoteSysContext db, IJWTService jwtService)
        {
            _db = db;
            _jwtService = jwtService;
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string Register(RegisterRequest request)
        {
            //密码需要二次确认
            if (request.Password != request.CirPassword)
                return "两次密码不一致";

            var salt = new Random().Next(1000, 9999);
            var md5 = MD5Helper.MD5Encrypt32(request.Password + salt.ToString());

            //判断重复号码
            if (_db.Users.Any(x => x.Email == request.Email))
                return "邮箱已存在";
            //添加信息到数据库 注册成功
            _db.Users.Add(new User
            {
                CreatTime = DateTime.Now,
                Email = request.Email,
                Password = md5,
                Salt = salt.ToString()
            });
            var row = _db.SaveChanges();
            if (row > 0)
                return "注册成功";
            return "注册失败";

        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string Login(LoginRequest request)
        {
            //1.使用手机号码去数据库查询用户
            var user = _db.Users.FirstOrDefault(x => x.Email == request.Email);

            //2.判断是否为空
            if (user == null)
                return "用户不存在";

            //生成MD5字符串
            var md5 = MD5Helper.MD5Encrypt32(request.Password + user.Salt);

            //判断是否密码正确
            if (user.Password != md5)
                return "密码不正确";
            //创建token
            return _jwtService.CreateToken(user.UserId, user.Email);
            return "登录成功";
        }
        /// <summary>
        /// 前端页面实现校验类别
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string test(int id)
        {
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var cate = _db.Categories.FirstOrDefault(x => x.CategoryId == id);
            var user = _db.Items.FirstOrDefault(x => x.UserId == Convert.ToInt32(userId) && x.Category == cate.Categoryname);
            //如果为空 表示该用户存在此类别的项目 只能查看资料 否则可以上传资料
            if (user != null)
                return "点击查看资料";
            return "点击上传资料";
        }
    }
}
